package com.xja.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xja.entity.Bookinfo;
import com.xja.entity.Orderinfo;
import com.xja.entity.PageResult;
import com.xja.entity.Userinfo;
import com.xja.feignclient.OrderService;
import com.xja.service.UserinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周广瑞
 * @date 2022/4/26 11:31
 */
@RestController
@RequestMapping("user")
//@CrossOrigin(allowCredentials = "true")
public class UserController {
    private Logger logger= LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private OrderService orderService;

    //timeSentinel

    @RequestMapping("findone")
    @SentinelResource(value = "timeSentinel", blockHandler = "findallSentinel")
    public PageResult<Userinfo> findone(@RequestParam("uid") Integer uid){
        logger.info("调用了图书业务中查询单个图书的功能");
        Userinfo userinfo =userinfoService.selectByPrimaryKey(uid);
        return PageResult.success("成功",userinfo);
    }
    public PageResult<Userinfo> findallSentinel(@RequestParam("uid") Integer uid, BlockException blockException){
        return PageResult.error("稍后访问",null);
    }

    @RequestMapping("find")
    public PageResult<List<Bookinfo>>findall(){
            logger.info("调用了订单业务查询所有的信息");
        List<Userinfo> userinfoList = userinfoService.slectUser();
        return PageResult.success("成功",userinfoList);
    }

    //查询用户以及用户的订单列表
    @RequestMapping("finduid")
    public PageResult<Map<String,Object>>findUid(@RequestParam("uid")Integer uid){
        logger.info("调用了查询用户以及用户订单的方法");
        Userinfo user = userinfoService.selectByPrimaryKey(uid);
        if (user ==null)
            return PageResult.nullresult("用户不存在",null);
        PageResult<List<Orderinfo>> pageResult = orderService.findUid(user.getUserid());
        List<Orderinfo> orderinfoList = pageResult.getObj();
        Map<String,Object>userMap = new HashMap<>();
        userMap.put("user",user);
        userMap.put("orderinfoList",orderinfoList);

        return PageResult.success("成功",userMap);
    }


    //按照用户id 查询用户信息
    @RequestMapping("finduserbyuid")
    public PageResult<Userinfo>findUserByUid(@RequestParam("uid")Integer uid){
        logger.info("调用了订单业务查询所有的信息");
                Userinfo user = userinfoService.selectByPrimaryKey(uid);
        return PageResult.success("成功",user);

    }


    //热点参数限流
    @SentinelResource(value ="userHotWord",fallback = "userHotWorFallback")
    @RequestMapping("userHotWord")
    public String userHotWprd(int userId){
        return "根据用户id调用方法"+userId;
    }
    public String userHotWorFallback(int userId) {
        return "服务降级当前异常太多。骚等重试。。。。。。。。";
    }

    @RequestMapping("method6")
    @SentinelResource(value = "sexdown" ,fallback = "method6fallback")
    public String method6(int uid){
        return "user.method6+"+uid;
    }
    public String method6fallback(int uid){
        return "user.method，授权不通过禁止访问";
    }

}
